
import codecs

import os

import numpy as np

from gensim import corpora
from gensim.models import LdaModel
from gensim.corpora import Dictionary

import pyLDAvis.gensim
import pyLDAvis
import pyLDAvis.gensim_models as gensimvis
from gensim.models import LdaModel
from gensim.corpora import Dictionary

train = []

fp = codecs.open('D:\\pycharmproject\\高校图书采购主题分析\\CNKI-output.txt','r',encoding='utf8')
for line in fp:
    if line != '':
        line = line.split()
        train.append([w for w in line])

dictionary = corpora.Dictionary(train)

corpus = [dictionary.doc2bow(text) for text in train]

lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, passes=60)
# num_topics：主题数目
# passes：训练伦次
# num_words：每个主题下输出的term的数目
'''
for topic in lda.print_topics(num_words = 20):
    termNumber = topic[0]
    print(topic[0], ':', sep='')
    listOfTerms = topic[1].split('+')
    for term in listOfTerms:
        listItems = term.split('*')
        print('  ', listItems[1], '(', listItems[0], ')', sep='')

print(type(vis_data))
print(type(lda))
print(type(corpus))
print(type(dictionary))
'''

# 准备可视化数据 换成 mmds 效果更好
vis_data = pyLDAvis.gensim.prepare(lda, corpus, dictionary, mds='mmds')

#直接显示
pyLDAvis.display(vis_data)


# 保存为 HTML 文件
#pyLDAvis.save_html(vis_data, 'D:\\pycharmproject\\高校图书采购主题分析\\ldavis.html')
# 打印当前工作目录 C:\Users\Evie
#print(os.getcwd())